package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.ExamPaperDTO;
import com.xuedongzhilian.dto.ExamQuestionDTO;
import com.xuedongzhilian.service.IExamPaperService;
import com.xuedongzhilian.service.IExamQuestionService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 试卷练习控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/study/exam")
public class ExamPaperController {

    @Resource
    private IExamPaperService examPaperService;
    
    @Resource
    private IExamQuestionService examQuestionService;

    /**
     * 创建试卷练习
     */
    @PostMapping
    public Result createExamPaper(@RequestBody ExamPaperDTO examPaperDTO) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        examPaperDTO.setUserId(userId);
        boolean success = examPaperService.createExamPaper(examPaperDTO);
        return success ? Result.ok() : Result.fail("创建试卷练习失败");
    }

    /**
     * 获取用户的试卷练习列表
     */
    @GetMapping
    public Result getExamPapers() {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        List<ExamPaperDTO> examPapers = examPaperService.getExamPapersByUserId(userId);
        return Result.ok(examPapers);
    }

    /**
     * 获取试卷练习详情
     */
    @GetMapping("/{paperId}")
    public Result getExamPaperDetail(@PathVariable("paperId") Long paperId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        ExamPaperDTO examPaperDTO = examPaperService.getExamPaperDetail(paperId, userId);
        return examPaperDTO != null ? Result.ok(examPaperDTO) : Result.fail("试卷练习不存在");
    }

    /**
     * 获取试卷的题目列表
     */
    @GetMapping("/{paperId}/questions")
    public Result getExamQuestions(@PathVariable("paperId") Long paperId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        // 验证试卷是否存在且属于当前用户
        ExamPaperDTO examPaperDTO = examPaperService.getExamPaperDetail(paperId, userId);
        if (examPaperDTO == null) {
            return Result.fail("试卷不存在");
        }
        
        List<ExamQuestionDTO> questions = examQuestionService.getExamQuestionsByPaperId(paperId);
        return Result.ok(questions);
    }

    /**
     * 更新试卷练习
     */
    @PutMapping
    public Result updateExamPaper(@RequestBody ExamPaperDTO examPaperDTO) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        examPaperDTO.setUserId(userId);
        boolean success = examPaperService.updateExamPaper(examPaperDTO);
        return success ? Result.ok() : Result.fail("更新试卷练习失败");
    }

    /**
     * 删除试卷练习
     */
    @DeleteMapping("/{paperId}")
    public Result deleteExamPaper(@PathVariable("paperId") Long paperId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = examPaperService.deleteExamPaper(paperId, userId);
        return success ? Result.ok() : Result.fail("删除试卷练习失败");
    }

    /**
     * 开始试卷练习
     */
    @PostMapping("/{paperId}/start")
    public Result startExamPaper(@PathVariable("paperId") Long paperId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = examPaperService.startExamPaper(paperId, userId);
        return success ? Result.ok() : Result.fail("开始试卷练习失败");
    }

    /**
     * 完成试卷练习
     */
    @PostMapping("/{paperId}/complete")
    public Result completeExamPaper(@PathVariable("paperId") Long paperId,
                                    @RequestParam Integer actualScore,
                                    @RequestParam Integer usedTime) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = examPaperService.completeExamPaper(paperId, userId, actualScore, usedTime);
        return success ? Result.ok() : Result.fail("完成试卷练习失败");
    }
}